var login = {
    events: {
    	'input,#loginBtn keydown':'enterSubmit',
        '.login-btn#loginBtn click': 'loginSubmit'
    },

    init: function(){
        var _this = this;
        Util.event.call(this, this.events);
        $("#loginBtn").focus();
        console.log(JSON.stringify(login.url.a))
    },
    url:{
        login:"http://" + window.location.host+"/gms/auth/login",
        logout:"http://" + window.location.host+"/gms/auth/logout",
        menu:"http://" + window.location.host+"/gms/auth/findAllMenu"
    },
    loginSubmit: function(){
        var _this = this;
        var username = $('input#userName').val(),password = $('input#password').val();
        if(!Util.isEmpty(username,password)){
            Util.ajaxRequest({
                url:_this.url.login,
                type:"post",
                autoError : false,
                data:{
                    userName: username,
                    password: password
                },
                dataType:"json",
                success:function(json){
                    if(json.code == 200){
                    	sessionStorage.setItem("userInfo",JSON.stringify(json.result));
                        _this.getMenu(function () {
                            window.location.href="index.html";
                        });
                    }else{
                       pop.alert('用户名和密码输入有误或不存在！');
                    }
                }
            });
        }else{
        	pop.alert('用户名或密码不能为空！');
        }
    },
    enterSubmit:function(ele,event){
        if(event.keyCode === 13){
            if($(".pop-box").length<1){
                this.loginSubmit();
            }
        }
    },
    logout: function () {
        var _this = this;
        Util.ajaxRequest({
            url:_this.url.logout,
            type:"post",
            autoError : false,
            dataType:"json",
            success:function(json){
                if(json.code == 200){
                    window.location.href="login.html";
                }else{
                    pop.alert("退出失败");
                }
            }
        });
    },
    getMenu: function (fn) {
        var _this = this;
        Util.ajaxRequest({
            url:_this.url.menu,
            type:"post",
            autoError : false,
            dataType:"json",
            success:function(json){
                if(json.code == 200){
                    sessionStorage.setItem("menuInfo",json.result);
                    $.isFunction(fn)?fn():"";
                }else{
                    pop.alert("菜单获取失败");
                }
            }
        });
    }
};
login.init();